gusucode.com > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM源码程序 > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM\stprtool\learning\unsuper\unsudemo.m
function []=unsudemo(action,hfigure,varargin) % UNSUDEMO demo on unsupervised (EM) learning algorithm. % % UNSUDEMO demonstrates the unsupervised (Expectation-Maximization) % learning algorithm on simple examples in 2D space. % % The aim is to estimate a statistical model described as the % weighted mixture of Gaussians from unlabeled data % (point set/patterns). The input data is supposed to be % identicaly and independently seleted. % % The found model is described by ellipsoids (shape of % covariances) and a croses (mean value vectors). The value % of the optimized log-likelihood function for given model % is displayed as well. % % Control: % Features - select the model with Independent (diagonal covariance % matrix) or Dependent (full cov. matrix) features. % % of classes - prescribe the number of classes (components of the % mixture of Gaussians). % % Iterations - number of iterations in one step. % Random init - the initial model is randomly generated and/or it is % determined by the first n-patterns (mean values) % and sample covarinace matrix (covarinaces matrices). % % FIG2EPS - export screen to the PostScript file. % Save model - save current model to file. % Load data - load input point sets from file. % Create data - invoke program for creating point sets. % Reset - set the tested algorithm to the initial state. % Play - run the tested algorithm. % Stop - stop the running algorithm. % Step - perform only one step. % Info - invoke the info box. % Close - close the program. % % See also UNSUNI, UNSUND. % % Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac % (c) Czech Technical University Prague, http://cmp.felk.cvut.cz % Written Vojtech Franc (diploma thesis) 10.11.1999 % Modifications % 11-june-2001, V.Franc, comments added. % 27.02.00 V. Franc % 5. 4.00 V. Franc % 23.06.00 V. Hlavac Comments polished. Message when no data loaded. % Export of the solution to global variables. % 27-mar-2001, V.Franc, Graph og log-likelihood function added % Used functions: PPOINTS, PNMIX % == Global variables, used to export results from UNSUDEMO ======== global UNSU_MI % K vectors with mean values global UNSU_SIGMA % K covariance matrices global UNSU_PK % K apriori probabilities for each distributions. %global UNSU_eI % Used only by the next iteration, not globalised global UNSU_solution % 1 if alg. finished in stationary point, 0 otherwise global UNSU_t % Number of iterations the algorithm performed % == Constants ===================================================== AXIST_ADD=10; AXISY_ADD=5; BORDER=0.25; % space betwean window outer and the points CENTERSIZE=10; % size of center point LINE_WIDTH=1; AXIST_ADD=10; DATA_IDENT='Finite sets, enumeration'; % file identifier randinit=1; if nargin < 1, action = 'initialize'; end % What action is required ? switch lower(action) case 'initialize' % == Initialize user interface control and figure window ================ % == Figure % ============================================================= left=0.2; width=0.6; bottom=0.1; height=0.8; hfigure=figure('Name','Unsupervised learning', ... 'Visible','off',... 'NumberTitle','off', ... 'Units','normalized', ... 'Position',[left bottom width height],... 'tag','Unsudemo',... 'doublebuffer','on',... 'backingstore','off'); % == Axes =============================================================== left=0.1; width=0.65; bottom=0.45; height=0.5; haxes1=axes(... 'Units','normalized', ... 'NextPlot','add',... 'UserData',[],... 'Position',[left bottom width height]); xlabel('feature x'); ylabel('feature y'); htitle1=title('No data loaded',... 'VerticalAlignment','bottom',... 'Parent',haxes1,... 'HorizontalAlignment','left',... 'Units','normalized',... 'Position',[0 1 0]); % axes log-Likelihood graph left=0.1; width=0.65; bottom=0.1; height=0.25; haxes2=axes(... 'Units','normalized', ... 'NextPlot','add',... 'Position',[left bottom width height]); ylabel('logL(t)'); htitle2=title('Log-likelihood function',... 'Parent',haxes2,... 'VerticalAlignment','bottom',... 'Units','normalized',... 'HorizontalAlignment','left',... 'Position',[0 1 0]); htxsteps=xlabel('step number t=0'); % == Comment Window frame ============================================== %% bottom=0.05; %% height=0.16; %% uicontrol( ... %% 'Style','frame', ... %% 'Units','normalized', ... %% 'Position',[left bottom width height], ... %% 'BackgroundColor',[0.5 0.5 0.5]); % Text label % uicontrol( ... % 'Style','text', ... % 'Units','normalized', ... % 'Position',[left height-0.01 width 0.05], ... % 'BackgroundColor',[0.5 0.5 0.5], ... % 'ForegroundColor',[1 1 1], ... % 'String','Comment Window'); % Edit window %% border=0.01; %% hconsole=uicontrol( ... %% 'Style','edit', ... %% 'HorizontalAlignment','left', ... %% 'Units','normalized', ... %% 'Max',10, ... %% 'BackgroundColor',[1 1 1], ... %% 'Position',[left+border bottom width-2*border height-0.05], ... %% 'Enable','inactive',... %% 'String',''); % == Buttons =========================================================== % -- Export to EPS --------- width=0.1; left=0.75-width; bottom=0.95; height=0.04; hbtclose = uicontrol(... 'Units','Normalized', ... 'Callback','fig2eps(gcf)',... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','FIG2EPS'); %---------------------------------- % Close button left=0.8; bottom=0.05; height=0.044; width=0.15; hbtclose = uicontrol(... 'Units','Normalized', ... 'Callback','close(gcf)',... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Close'); % Info button: call stanard info box bottom=bottom+1.5*height; hbtinfo = uicontrol(... 'Units','Normalized', ... 'Callback','unsudemo(''info'',gcf)',... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Info'); % Step button: perform one adaptation step bottom=bottom+1.5*height; hbtstep = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Step', ... 'Interruptible','off',... 'Callback','unsudemo(''step'',gcf)'); % Stop button: stop process of adaptation bottom=bottom+height; hbtstop = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Stop', ... 'Callback','set(gcbo,''UserData'',1)',... 'Enable','off'); % Play button: begin adaptation bottom=bottom+height; hbtplay = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Play', ... 'Callback','unsudemo(''play'',gcf)'); % Reset button: set up t = 0 bottom=bottom+height; hbtreset = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Reset', ... 'Callback','unsudemo(''reset'',gcf)'); % Create data bottom=bottom+1.5*height; hbtcreat = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Create data', ... 'Callback','unsudemo(''creatdata'',gcf)'); % Load data bottom=bottom+1*height; hbtload = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Load data', ... 'Callback','unsudemo(''getfile'',gcf)'); % Save model bottom=bottom+1.5*height; hbtSaveModel = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Save model', ... 'Callback','unsudemo(''savemodel'',gcf)'); % Load model % bottom=bottom+1*height; % hbtLoadModel = uicontrol(... % 'Units','Normalized', ... % 'ListboxTop',0, ... % 'Position',[left bottom width height], ... % 'String','Load model', ... % 'Callback','unsudemo(''loadmodel'',gcf)'); % == PopUp Menu ===================================================== bottom=0.95-height; htxfeatures=uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left bottom width height], ... 'String','Features'); % popup menu bottom=bottom-height; hpufeatures=uicontrol( ... 'Style','popup', ... 'Units','normalized', ... 'Position',[left bottom width height], ... 'String',['Independent ';'Dependent ']); % == Edit line ========================================================== % prior info about number of the classes bottom=bottom-1.3*height; htxclasses=uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left bottom width 0.9*height], ... 'String','# of classes'); bottom=bottom-height; hedclasses = uicontrol(... 'Units','normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'Style','edit',... 'String','2'); % Iterations bottom=bottom-1.3*height; htxiter=uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left bottom width 0.9*height], ... 'String','Iterations'); bottom=bottom-height; hediter = uicontrol(... 'Units','normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'Style','edit',... 'String','1'); % == CheckBox ========================================================= % Should the first algorithm step be random or not ? bottom=bottom-height*1.3; hxbrandom = uicontrol(... 'Style','checkbox', ... 'Units','normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Random init'); %===================================================================== % Store handlers handlers=struct(... 'ellipse',struct('handler',-1,'mi',[],'sigma',[],'t',0,'Pk',[],... 'solution',0),... 'center',[],... 'graph1',struct('handler',-1,'loglik',[],'axist',0,'time',[]),... 'title1',htitle1,... 'title2',htitle2,... 'btSaveModel',hbtSaveModel,...% 'btLoadModel',hbtLoadModel,... 'btstep',hbtstep,... 'btstop',hbtstop,... 'btclose',hbtclose,... 'btplay',hbtplay,... 'btreset',hbtreset,... 'btinfo',hbtinfo,... 'btload',hbtload,... 'btcreat',hbtcreat,... %%% 'console',hconsole,... 'txsteps',htxsteps,... 'txclasses',htxclasses,... 'txiter',htxiter,... 'txfeatures',htxfeatures,... 'pufeatures',hpufeatures,... 'editer',hediter,... 'xbrandom',hxbrandom,... 'axes1',haxes1,... 'axes2',haxes2,... 'edclasses',hedclasses); set(hfigure,'UserData',handlers) % Reset unsudemo('reset',hfigure); % Put figure on desktop set(hfigure,'Visible','on'); drawnow; case 'savemodel' % == Save model ============================================ h=get(hfigure,'UserData'); if h.ellipse.t == 0, errordlg('No model has found yet.','No model to save','modal'); return; end [name,path]=uiputfile('*.mat','Save model'); if name ~= 0, MI=h.ellipse.mi; % mean values SIGMA=h.ellipse.sigma; % covariance matrices Pk=h.ellipse.Pk; % a-priory probabilities solution=h.ellipse.solution; steps=h.ellipse.t; % number of performed steps I=[1:size(MI,2)]; N=size(MI,1); id=dataid(2); K=ones(1,size(MI,2)); save(strcat(path,name),'id','K','N','I',... 'steps','solution','Pk','SIGMA','MI'); end case 'loadmodel' % == Load model ============================================ h=get(hfigure,'UserData'); sets=get(h.axes1,'UserData'); % Are data sets loaded ? if isempty(sets)==1, % no warning is needy because of the huge topic on the screen:))). return; end [name,path]=uigetfile('*.mat','Load model'); if name ~= 0, fname=strcat(path,name); % if checkdat(fname,DATA_IDENT,2,0)==1, unsudemo('reset',hfigure); model=load(fname); if exist('model.Pk')==0, % suppose uniformly distributed Pk model.Pk=ones(1,sum(model.K))/sum(model.K); end h.ellipse.mi = model.MI; h.ellipse.sigma=model.SIGMA; h.ellipse.Pk = model.Pk; h.ellipse.solution=0; h.ellipse.t=0; set(h.edclasses,'String',num2str(sum(model.K))); h.ellipse.classes=sum(model.K); h.ellipse.features=get(h.pufeatures,'Value'); set(hfigure,'UserData',h); unsudemo('step',hfigure); % else % errordlg('This file does not contain required data.','Bad file','modal'); % end end case 'play' % == Play ============================================ h=get(hfigure,'UserData'); % get data set sets=get(h.axes1,'UserData'); % Are data sets loaded ? if isempty(sets)==1 | h.ellipse.solution==1, %% text=sprintf(... %% 'No action performed. No data to work on. Load or create it!'); %% set(h.console,'String',text); return; end % disable button set([h.editer,h.btstep,h.btclose,h.btplay,... h.btreset,h.btinfo,h.btload,h.btcreat,h.txiter],... 'Enable','off'); % enable stop button set(h.btstop,'Enable','on'); % get # of iterations iter=str2num(get(h.editer,'String')); % # of classes if h.ellipse.t==0, h.ellipse.classes=str2num(get(h.edclasses,'String')); h.ellipse.features=get(h.pufeatures,'Value'); set([h.xbrandom,h.edclasses,h.txclasses,h.pufeatures,h.txfeatures],... 'Enable','off'); end % Shall the init be random, yes or no ? randinit=get(h.xbrandom,'Value'); % set stop button set(h.btstop,'UserData',0); % Play - adaptation process play=1; % flag 1 - not finished, 1 - finished; while play==1 & get(h.btstop,'UserData')==0, % perform one learning step if h.ellipse.features==2, % correlated features [h.ellipse.mi,h.ellipse.sigma,h.ellipse.Pk,eI,h.ellipse.solution,... h.ellipse.t]=unsund(sets.X,h.ellipse.classes,iter,randinit,... h.ellipse.t,h.ellipse.mi, h.ellipse.sigma,h.ellipse.Pk); else % independent [h.ellipse.mi,h.ellipse.sigma,h.ellipse.Pk,eI,h.ellipse.solution,... h.ellipse.t]=unsuni(sets.X,h.ellipse.classes,iter,randinit,... h.ellipse.t,h.ellipse.mi,h.ellipse.sigma,h.ellipse.Pk); end text=sprintf('step number t=%d ',h.ellipse.t); if h.ellipse.solution==1, %% text=strvcat(text,'Solution was found.'); text=[text ',solution was found']; play=0; set(h.txsteps,'String',text); else set(h.txsteps,'String',text); val= mln(sets.X,h.ellipse.mi,h.ellipse.sigma,h.ellipse.Pk); text=sprintf('Log-likelihood, logL(t) = %f',val); set(h.title2,'String',text); h.graph1.time=[h.graph1.time,h.ellipse.t]; h.graph1.loglik=[h.graph1.loglik,val]; ylimit=get(h.axes2,'YLim'); if ylimit(2) < val, set(h.axes2,'YLim',[ylimit(1) val+AXISY_ADD]); end % is axis to be changed ? if h.ellipse.t > h.graph1.axist, h.graph1.axist=h.ellipse.t+iter*AXIST_ADD; set(h.axes2,'XLim',[1 h.graph1.axist]); end set(h.graph1.handler,'XData',h.graph1.time,'YData',h.graph1.loglik,... 'Visible','on'); if h.ellipse.handler==-1, axes(h.axes1); end [h.ellipse.handler,h.center]=... pnmix(sets.X,h.ellipse.mi,h.ellipse.sigma,eI,h.ellipse.handler,h.center); end % comment %% set(h.console,'String',text); % store data set(hfigure,'UserData',h); % flush it on desktop drawnow; end % of while % dissable button set([h.editer,h.btstep,h.btclose,h.btplay,... h.btreset,h.btinfo,h.btload,h.btcreat,h.txiter],... 'Enable','on'); % enable stop button set(h.btstop,'Enable','off'); % copy the solution to global variables to be visible outside unsudemo UNSU_MI = h.ellipse.mi; UNSU_SIGMA = h.ellipse.sigma; UNSU_PK = h.ellipse.Pk; UNSU_solution = h.ellipse.solution; UNSU_t = h.ellipse.t; case 'step' % == One step of learning =========================================== h=get(hfigure,'UserData'); % get data set sets=get(h.axes1,'UserData'); % are data sets loaded ? if isempty(sets)==1 | h.ellipse.solution==1, %%text=sprintf('No action performed. No data to work on. Load or create it!'); %% set(h.console,'String',text); %% set(h.txsteps,'String',text); return; end % get # of iter iter=str2num(get(h.editer,'String')); % # of classes if h.ellipse.t==0, h.ellipse.classes=str2num(get(h.edclasses,'String')); h.ellipse.features=get(h.pufeatures,'Value'); set([h.xbrandom,h.edclasses,h.txclasses,h.pufeatures,h.txfeatures],'Enable','off'); end % random init yes or no randinit=get(h.xbrandom,'Value'); % perform one learning step if h.ellipse.features==2, % correlated [h.ellipse.mi,h.ellipse.sigma,h.ellipse.Pk,eI,h.ellipse.solution,... h.ellipse.t]=unsund(sets.X,h.ellipse.classes,iter,randinit,... h.ellipse.t,h.ellipse.mi,h.ellipse.sigma,h.ellipse.Pk); else % independent [h.ellipse.mi,h.ellipse.sigma,h.ellipse.Pk,eI,h.ellipse.solution,... h.ellipse.t]=unsuni(sets.X,h.ellipse.classes,iter,randinit,... h.ellipse.t,h.ellipse.mi,h.ellipse.sigma,h.ellipse.Pk); end text=sprintf('step number t=%d ',h.ellipse.t); if h.ellipse.solution==1, %% text=strvcat(text,'Solution is found.'); text=[text ',solution was found']; set(h.txsteps,'String',text); else set(h.txsteps,'String',text); val= mln(sets.X,h.ellipse.mi,h.ellipse.sigma,h.ellipse.Pk); text=sprintf('Log-likelihood, logL(t) = %f',val); set(h.title2,'String',text); h.graph1.time=[h.graph1.time,h.ellipse.t]; h.graph1.loglik=[h.graph1.loglik,val]; ylimit=get(h.axes2,'YLim'); if ylimit(2) < val, set(h.axes2,'YLim',[ylimit(1) val+AXISY_ADD]); end % is axis to be changed ? if h.ellipse.t > h.graph1.axist, h.graph1.axist=h.ellipse.t+iter*AXIST_ADD; set(h.axes2,'XLim',[1 h.graph1.axist]); end set(h.graph1.handler,'XData',h.graph1.time,'YData',h.graph1.loglik,... 'Visible','on'); if h.ellipse.handler==-1, axes(h.axes1); end [h.ellipse.handler,h.center]=... pnmix(sets.X,h.ellipse.mi,h.ellipse.sigma,eI,h.ellipse.handler,h.center); end % comment %% set(h.console,'String',text); % flush it on desktop drawnow; set(hfigure,'UserData',h); % copy the solution to global variables to be visible outside unsudemo UNSU_MI = h.ellipse.mi; UNSU_SIGMA = h.ellipse.sigma; UNSU_PK = h.ellipse.Pk; UNSU_solution = h.ellipse.solution; UNSU_t = h.ellipse.t; case 'getfile' % == Invoke standard open file dialog ==================================== % Opens file and checks if contains apropriate data, if yes than loads data. h=get(hfigure,'UserData'); % change path to directory %% wres=what('unsuper'); %% cd(wres.path); [name,path]=uigetfile('*.mat','Open file'); if name~=0, file.pathname=strcat(path,name); file.path=path; file.name=name; if checkdat(file.pathname,DATA_IDENT,2,0)==1, set(h.btload,'UserData',file); unsudemo('loadsets',hfigure); else errordlg('This file does not contain required data.','Bad file','modal'); end end case 'loadsets' % == Load sets ================================================================== % Get file name from the pop up menu according to menu pointer. h=get(hfigure,'UserData'); % Get file name with sets file=get(h.btload,'UserData'); % Load sets sets=load(file.pathname); % store loaded sets set(h.axes1,'UserData',sets); % call reset unsudemo('reset',hfigure); drawnow; case 'reset' % == Reset adaptation process, set up t=0 ================ h=get(hfigure,'UserData'); % get handlers % get file file=get(h.btload,'UserData'); % get data set sets=get(h.axes1,'UserData'); % zeroes parameters of the separation line h.ellipse.mi=[]; h.ellipse.sigma=[]; h.ellipse.t=0; h.ellipse.Pk=[]; h.center=-1; h.ellipse.handler=-1; h.ellipse.solution=0; h.graph1.time=[]; h.graph1.axist=0; h.graph1.loglik=[]; % clear axes prob. clrchild(h.axes2); axes(h.axes2); %% setaxis(h.axes2,[0 1 0 1]); axis auto; h.graph1.handler=plot([0],[0],'b','Parent',h.axes2,... 'EraseMode','background','Visible','off'); % store h set(hfigure,'UserData',h); % enable Edit Line 'classes' set([h.xbrandom,h.edclasses,h.txclasses,h.pufeatures,h.txfeatures],'Enable','on'); % comment text=sprintf('Log-likelihood, logL(t)'); set(h.title2,'String',text); text=sprintf('step number t=0. '); set(h.txsteps,'String',text); % clears axes set(get(h.axes1,'Children'),'EraseMode','normal'); %%% cla; clrchild(h.axes1); drawnow; % set axes and plot mixture axes(h.axes1); if isempty(sets)==0, win=cmpwin(min(sets.X'),max(sets.X'),BORDER,BORDER); %%% axis(win); setaxis(h.axes1,win); %% axes(h.axes1); ppoints(sets.X,sets.I); end %%%%%%%%%%%%%%%%%%%%%%%%%%%% % create comment if isempty(sets)==0, set(h.title1,'String',sprintf('File: %s, # of points K = %d',file.name,sum(sets.K))); else set(h.title1,'String','No data loaded'); pos=get(h.axes1,'Position'); fsize=min(pos(3),pos(4))/7; setaxis(h.axes1,[-1 1 -1 1]); builtin('text',0,0,'Press ''Load data'' button.',... 'Parent',h.axes1,... 'HorizontalAlignment','center',... 'FontUnits','normalized',... 'Clipping','on',... 'FontSize',fsize); end drawnow; case 'creatdata' % == Invoke data set creator ============================================ creatset('finite',10,'unsudemo','created',hfigure); case 'created' % == Load new created data set =========================================== % get handler and make this figure active figure(hfigure); h=get(hfigure,'UserData'); % get file name path=varargin{1}; name=varargin{2}; pathname=strcat(path,name); if checkdat(pathname,DATA_IDENT,2,0)==1, file.pathname=pathname; file.path=path; file.name=name; set(h.btload,'UserData',file); unsudemo('loadsets',hfigure); else errordlg('This file does not contain required data.','Bad file','modal'); end case 'info' % == Call standard Matlab`s info box ========================================= helpwin(mfilename); end % of switch